<?php

namespace App\Http\Controllers\Home\Set;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Home\Users as UsersModel;
use DownloadLog;
use Tools;
use Users;
use Frame;
use Conf;

class DownloadController extends Controller
{
    protected $passField = [
        'download'
    ];

    public function download_list(Request $request)
    {
        $download_list = Frame::GetBranchAll(config('base.branch.lg.id'));

        return view('Home.User.Set.Download.download',compact('download_list'));
    }

    public function download_edit(UsersModel $obj)
    {
        if(request()->isMethod('post')){
            //验证
            $this->validate(request(), [
                'download' => 'required|integer',
            ],[],[
                'download' => '下载量',
            ]);

            //数据库操作
            foreach (request()->all() as $key => $val){
                if (!is_numeric($val)){	$val = clean($val);}
                if(in_array($key,$this->passField) && trim($val)){
                    $obj[$key] = $val;
                }
            }

            $obj->save();
            //返回结果
            return ['code'=>1];
        }

        return view('Home.User.Set.Download.edit',compact('obj'));

    }

    public function download_user_list($uid,Request $request)
    {
        $page = $request->input('page');

        $page_info = Tools::page_helper($page,config('base.home_limit.base_limit.render_limit'),config('base.home_limit.base_limit.limit'));

        $opt['uid'] = $uid;

        $list = DownloadLog::getList($page_info['skip'],$page_info['limit'],$opt);

        $count = DownloadLog::getCount($opt);

        $num = intval(ceil($count/config('base.home_limit.base_limit.limit')));

        return view('Home.User.Set.Download.download_user_list',compact('list','uid','page','num'));
    }


}